<template>
	<view class="container">
		<view v-if="posts.length === 0" class="no-data">
			暂无留言
		</view>
		<view v-else class="post-list">
			<view v-for="post in posts" :key="post.id" class="post-item">
				<view class="post-title">{{ post.title }}</view>
				<view class="post-content">{{ post.content }}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				posts: []
			};
		},
		onLoad() {
			this.fetchPosts();
		},
		methods: {
			async fetchPosts() {
				try {
					const response = await uni.request({
						url: 'http://localhost:9090/posts', // 替换为你的后端API地址
						method: 'GET'
					});
					if (response.statusCode === 200) {
						this.posts = response.data.data;
					} else {
						uni.showToast({
							title: '获取留言失败',
							icon: 'none'
						});
					}
				} catch (error) {
					console.error('获取留言失败:', error);
					uni.showToast({
						title: '获取留言失败',
						icon: 'none'
					});
				}
			}
		}
	};
</script>

<style>
	.container {
		padding: 20px;
	}

	.no-data {
		text-align: center;
		margin-top: 50px;
		color: #999;
	}

	.post-list {
		display: flex;
		flex-direction: column;
	}

	.post-item {
		margin-bottom: 20px;
		padding: 15px;
		background-color: #f8f8f8;
		border-radius: 8px;
	}

	.post-title {
		font-size: 18px;
		font-weight: bold;
		margin-bottom: 10px;
	}

	.post-content {
		font-size: 14px;
		color: #666;
		margin-bottom: 10px;
	}
</style>